package a66weding.com.jiehuntong.pager;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import a66weding.com.jiehuntong.MainActivity;
import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.base.BaseActivity;

/**
 * Created by admin on 2016/7/4.
 */
public class PageUiManager  implements IContant,MainActivity.OnBackListener {
    private Activity mActivity;
    private View view;
    private BasePager basePager;
    private List<View> pageViews;
    private LayoutInflater inflater;
    private ViewPager mViewPager;
    public PageUiManager(Activity mActivity,View view){
        this.mActivity = mActivity;
        this.view = view;
        this.inflater = LayoutInflater.from(mActivity);
        initView();
    }

    /**
     */
    private void initView() {

        pageViews = new ArrayList<View>();
        mViewPager.addOnPageChangeListener(new MyOnPageChange());


    }

    @Override
    public void onBack() {
        if(mViewPager.isShown()){
            mViewPager.setCurrentItem(0,true);
        }
    }

    private class MyOnPageChange implements ViewPager.OnPageChangeListener {
        int onPageSelected = -1;
        //监听滑动
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }
        //监听选中页面
        @Override
        public void onPageSelected(int position) {
            onPageSelected = position;
        }
        //监听viewPage的3个状态 ： 停止  拖拽  设置中
        @Override
        public void onPageScrollStateChanged(int state) {
            if(state == ViewPager.SCROLL_STATE_IDLE && onPageSelected ==0){
                //销毁 ViewPager中的所有页面
                mViewPager.removeAllViews();
                mViewPager.setVisibility(View.INVISIBLE);
            }
        }

    }

    //ViewPager的适配器
    private  class MyViewPagerAdapter extends PagerAdapter {

        private List<View> views;

        public MyViewPagerAdapter(List<View> views) {
            this.views = views;
        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {

            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(views.get(position));
            return views.get(position);
        }

    }

    public void  setContentType(int form){
     //注册监听;
        switch (form){
            //设置帮助页面;
            case HELP_FRIENDS:
                //帮助碎片
                HelpPage helpPage = new HelpPage(mActivity,this);
                //viewpage 显示
                //ViewPager的第1页：透明的
                View view1 = inflater.inflate(R.layout.viewpager_trans_layout, null);
                //ViewPage的第2页：音乐列表
                View view2 = helpPage.getView();
                //listView清空
                pageViews.clear();
                pageViews.add(view1);
                pageViews.add(view2);
                //设置默认显示第2页 下表是1
                mViewPager.setAdapter(new MyViewPagerAdapter(pageViews));
                mViewPager.setVisibility(View.VISIBLE);
                mViewPager.setCurrentItem(1, false);
                break;
            //修改地址
            case SET_ADDRESS:
                LocalPager localPager = new LocalPager(mActivity,this);
                View view3 = inflater.inflate(R.layout.viewpager_trans_layout, null);
                View view4 = localPager.getView();
                //listView清空
                pageViews.clear();
                pageViews.add(view3 );
                pageViews.add(view4 );
                //设置默认显示第2页 下表是1
                mViewPager.setAdapter(new MyViewPagerAdapter(pageViews));
                mViewPager.setVisibility(View.VISIBLE);
                mViewPager.setCurrentItem(1, false);
                break;

        }
    }
}
